package handlers

import (
	"1-13zy/api/request"
	"1-13zy/api/response"
	"1-13zy/dao"
	"1-13zy/models"
	"github.com/gin-gonic/gin"
)

func AddArticle(c *gin.Context) {
	var data request.AddArticle
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	article := models.Article{
		Title:   data.Title,
		FileUrl: data.FileUrl,
		Intro:   data.Intro,
	}
	err = dao.AddArticle(&article)
	if err != nil {
		response.ResponseDataError(c, "添加失败")
		return
	}
	response.ResponseDataSuccess(c, article.Id)

}

func AddArticleGood(c *gin.Context) {
	var data request.AddArticleGood
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	good := models.ArticleGood{
		ArticleId: data.ArticleId,
		UserId:    data.UserId,
	}
	err = dao.AddArticleGood(&good)
	if err != nil {
		response.ResponseDataError(c, "添加失败")
		return
	}
	response.ResponseDataSuccess(c, good.Id)
}

func AddArticleReview(c *gin.Context) {
	var data request.AddArticleReview
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	review := models.ArticleReview{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		Context:   data.Context,
		FathId:    0,
	}
	err = dao.AddArticleReview(&review)
	if err != nil {
		response.ResponseDataError(c, "添加失败")
		return
	}
	response.ResponseDataSuccess(c, review)
}
func ResAddArticleReview(c *gin.Context) {
	var data request.ResAddArticleReview
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	review := models.ArticleReview{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		Context:   data.Context,
		FathId:    1,
	}
	err = dao.ResAddArticleReview(&review)
	if err != nil {
		response.ResponseDataError(c, "添加失败")
		return
	}
	response.ResponseDataSuccess(c, review)
}

func ReviewGood(c *gin.Context) {
	var data request.ReviewGood
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	good := models.ReviewGood{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		ReviewId:  data.ReviewId,
	}
	err = dao.ReviewGood(&good)
	if err != nil {
		response.ResponseDataError(c, "添加失败")
		return
	}
	response.ResponseDataSuccess(c, good.Id)
}

func ReviewDel(c *gin.Context) {
	var data request.ArticleReviewDel
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	del, err := dao.ReviewDel(data.Id)
	if err != nil {
		response.ResponseDataError(c, "删除失败")
		return
	}
	response.ResponseDataSuccess(c, del)
}

func ReviewGoodDel(c *gin.Context) {
	var data request.ReviewGoodDel
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	del, err := dao.ReviewGoodDel(data.Id)
	if err != nil {
		response.ResponseDataError(c, "取消点赞失败")
		return
	}
	response.ResponseDataSuccess(c, del.Id)
}
